﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Microsoft.Practices.EnterpriseLibrary.Data;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Reflection;
using System.Threading;

namespace Job.Core
{
    public class BaseBLL<T> where T : new()
    {
        public static T _this;
        public static object _lock = new object();
        public static T Instance()
        {
            if (_this == null)
            {
                lock (_lock)
                {
                    if (_this == null)
                    {
                        _this = new T();
                    }
                }
            }
            return _this;
        }


        public string strMsgs = "";
        public int intLogTimes = 10;    //多少条日志开始写记录
        public int intLogCount = 0;
        public StringBuilder strLog = new StringBuilder();
        public string strLogMart = "{0,-16} {1}\r\n\r\n";
        public string strLogPath = @"E:\log";
        public string strLogFile = @"Log";
        public ThreadStatus ThreadStats = ThreadStatus.Stop;

        /// <summary>
        /// 开启服务
        /// </summary>
        public virtual void Start()
        {
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        public virtual void Stop()
        {
        }

        /// <summary>
        /// 服务控制台
        /// </summary>
        public virtual void Control(ThreadStatus spider)
        {
            ThreadStats = spider;
        }

        /// <summary>
        /// 是否是需要休眠
        /// </summary>
        public void Sleep()
        {
            if (ThreadStats == ThreadStatus.Sleep)
            {
                Thread.Sleep(1000);
                Sleep();
            }
        }

        /// <summary>
        /// log 对应服务员的log文件夹
        /// </summary>
        public string LogFile
        {
            get { return strLogFile; }
            set { strLogFile = value; }
        }

        /// <summary>
        /// log 目录 eg. E:\log
        /// </summary>
        public string LogPath
        {
            get { return strLogPath; }
            set { strLogPath = value; }
        }

        /// <summary>
        /// log 文件路径 ==  总路径 + 目录 + date
        /// </summary>
        public string GetLogPath
        {
            get { return string.Format(@"{0}\{1}\{2}.txt", LogPath, LogFile, DateTime.Now.ToString("yyyy-MM-dd")); }
        }

        /// <summary>
        /// 缓存日志 下记录到内存中 服务停止时写日志
        /// </summary>
        public void SpiderLog(string strMsg)
        {
            SpiderLog(strMsg, false);
        }

        /// <summary>
        /// 缓存日志\写日志
        /// </summary>
        public void SpiderLog(string strMsg, bool isLog)
        {
            intLogCount++;
            strMsgs = strMsg;
            strLog.AppendFormat(strLogMart, strMsg, DateTime.Now);
            if (isLog || intLogCount >= intLogTimes || ThreadStats == ThreadStatus.Stop)
            {
                FileHelper.Write(strLog.ToString(), GetLogPath, true, "UTF-8");
                strLog = new StringBuilder();
                intLogCount = 0;
            }
        }
    }
}
